<?php

/**
 */

namespace App\Api\CatchOrder;

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Support\Facades\DB;
use App\Repository\UpThree\MainOrderRepostition;
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;

class Pdd
{
    protected $log;
    protected $root;
    protected $config;
    protected $version;
    protected $url_code;
    protected $url_api;
    protected $access_token;
    protected $client_id;
    protected $interval_time;
    protected $interval_time_int;
    private $return_url;
    private $client_secret;
    private $miss_secret;
    private $shopArr;
    private $pdd_id;
    public function __construct($access_token, $client_id, $client_secret = '', $miss_secret = '', $id = 0, $time = 30)
    {
        $this->config = config('pdd');
        $this->log = new Logger('Pdd');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/Pdd.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );

        $this->root = new Logger('PddRootLog');
        $this->root->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/PddRootLog.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->version = 'V1'; // 版本
        $this->url_code = 'https://fuwu.pinduoduo.com/service-market/auth';  // 获取code
        $this->url_api = 'http://gw-api.pinduoduo.com/api/router'; // 请求连接
        $this->return_url = 'https://adminnew.chenqiwangluo.com/api/v1/getPddCode';
        $this->access_token = $access_token;
        $this->client_secret = $client_secret;
        $this->miss_secret = $miss_secret;
        $this->client_id = $client_id;
        $this->pdd_id = $id;
        if (!empty($id)) {
            $shop = DB::table('pdd_conf')->where(['id' => $id])->first();
            if ($shop) {
                $this->shopArr = json_decode(json_encode($shop), true);
            }
        }
        $this->interval_time = $time * 60;
        $this->interval_time_int = 60 * 3; // 往前 或 往后 差个 一分钟 以防漏单
    }

    /**
     * 同步数据
     *
     * @return void
     */
    public  function async()
    {
        if (empty($this->shopArr)) {
            return ['code' => 103, 'msg' => '参数错误,请联系管理员'];
        }
        // 获取增量订单
        set_time_limit(300);
        $time2  =  time();

        if (!empty($this->shopArr['mess_secret'])) {
            $time2 = ($this->shopArr['mess_secret'] + $this->interval_time);
        }
        // $time3  =   strtotime("-12 hour");
        // 间隔时间
        $time1  =   $time2 - $this->interval_time;
        $time2 = time();

        /* $res    =   DB::table('flow_order_async')->where([['async_time', '>', $time1]])->first();
        if($res){ return true; }*/

        // $url    =   $this->config['url_api'];
        $url = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['end_updated_at']     =   $time2;
        $data['is_lucky_flag']      =   '0';
        $data['order_status']       =   '1';
        $data['page']               =   '1';
        $data['refund_status']      =   '1';
        $data['trade_type']      =   '0';
        $data['start_updated_at']   =   $time1;
        $data['timestamp']          =   $time2;
        $data['type']               =   'pdd.order.number.list.increment.get';
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);

        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        // var_dump($content);
        $this->root->info('传递的参数:' . json_encode($data) . '同步订单库---' . $content);
        $json       =   json_decode($content, true);
        DB::table('pdd_conf')->where('id', $this->pdd_id)->update(['mess_secret' => $time2]);
        // 获取到订单
        $this->root->info('同步订单库---' . $content);
        if (!$json || !isset($json['order_sn_increment_get_response']) || !isset($json['order_sn_increment_get_response']['total_count']) || $json['order_sn_increment_get_response']['total_count'] < 1) {
            $this->root->info('同步订单库失败---' . $content);
            return false;
        }
        $add    =   [];
        $model = new MainOrderRepostition();
        $allList = [];
        // 循环一遍 为得到解密的数据
        $getAllData = $json['order_sn_increment_get_response']['order_sn_list'];
        $a1 = [];
        foreach ($getAllData as $keys => $n) {
            if (!isset($n['order_sn'])) {
                $this->root->info('缺少订单信息---');
                continue;
            }
            if (DB::table('pdd_catch')->where('dist_orderno', $n['order_sn'])->first()) {
                continue;
            }
            $a = $n;
            $orderInfo  =   $this->getOrderInfo($n['order_sn']);
            if (!$orderInfo) {
                $this->root->info('获取用户身份信息失败---' . $n['order_sn']);
                continue;
            }
            $a['id_card_name'] = $orderInfo['id_card_name'];
            $a['id_card_num'] = $orderInfo['id_card_num'];
            // var_dump($allList, 666666666);
            $getUserInfos = $this->getAllUserInfo($a, $allList);
            $getUserInf = $getUserInfos['data'];
            $a['true_name'] = $getUserInf['true_name'];
            $a['idcard'] = $getUserInf['idcard'];
            $a['phone'] = $getUserInf['phone'];
            $a['addr'] = $getUserInf['addr'];
            $a['name'] = $getUserInf['name'];
            $a1[] = $a;
            $allList = $getUserInfos['allList'];
        }
        // var_dump($allList, 7777777777);
        // 发送请求 获取解密后的数据
        $allListDesc = $this->getUserInfo($allList);
        // var_dump($allList, 111111111);
        // var_dump($allListDesc, 222222222);
        // die();
        foreach ($a1 as $n) {

            if (!isset($n['order_sn'])) {
                $this->root->info('缺少订单信息---');
                continue;
            }
            if (DB::table('pdd_catch')->where('dist_orderno', $n['order_sn'])->first()) {
                continue;
            }
            // $orderInfo  =   $this->getOrderInfo($n['order_sn']);
            // if (!$orderInfo) {
            //     $this->root->info('获取用户身份信息失败---' . $n['order_sn']);
            //     continue;
            // }
            // $n['id_card_name'] = $orderInfo['id_card_name'];
            // $n['id_card_num'] = $orderInfo['id_card_num'];
            // $userInfo   =   $this->getUserInfo($n);
            $userInfo = $this->getUserInfoDesc($n, $allListDesc);
            $staus = '0';
            if (!$userInfo) {
                $staus = '60';
                $stausDesc = '失败单';
                $this->root->info('解密失败---' . $n['order_sn']);
                // continue;
            }
            // var_dump($n, 5555555555);

            $time   =   date('Y-m-d H:i:s');
            // if (!DB::table('pdd_catch')->where('dist_orderno', $n['order_sn'])->first()) {
            //     if(){

            //     }

            // }
            $insertOrder = [
                'dist_orderno' => $n['order_sn'],
                'dist_id' => $this->shopArr['id'],
                'dist_name' => $this->shopArr['remark'],
                'goods_code' => $n['item_list'][0]['outer_id'],
                'name' => !empty($userInfo['true_name']) ? $userInfo['true_name'] : '',
                'identity' => !empty($userInfo['idcard']) ? $userInfo['idcard'] : '',
                'contact' => !empty($userInfo['phone']) ? $userInfo['phone'] : '',
                'province' => $n['province'],
                'city' =>  $n['city'],
                'area' => $n['town'],
                'address' => !empty($userInfo['addr']) ? $userInfo['addr'] : '',
                'create_at' => date('Y-m-d H:i:s'),
                'created_at' => $n['created_time'],
                'shop_id' => $this->shopArr['shop_id']
            ];
            // 插入订单表中

            if (empty($staus) || $staus != 60) {
                $insertOrder['source'] = 11;
                $res = $model->orderSubmit($insertOrder);
                $this->log->info('拼多多参数:' . json_encode($insertOrder) . '返回参数:' . json_encode($res));
            }
            $insertOrder['status'] = !empty($staus) ? $staus : '0';
            $insertOrder['status_desc'] = !empty($stausDesc) ? $stausDesc : '提交中';
            if (!empty($res['code']) && $res['code'] == 200) {
                $insertOrder['status'] = 1;
                $insertOrder['status_desc'] = '提交上游';
                $insertOrder['three_msg'] = !empty($res['msg']) ? $res['msg'] : '提交异常';
            }
            if (empty($res['code']) || $res['code'] != 200) {
                $insertOrder['status'] = 50;
                $insertOrder['status_desc'] = '提交失败';
                $insertOrder['three_msg'] = !empty($res['msg']) ? $res['msg'] : '提交异常';
            }
            if (!empty($insertOrder['source'])) {
                unset($insertOrder['source']);
            }
            $add[] = $insertOrder;
        }
        if ($add) DB::table('pdd_catch')->insert($add);

        return 1;
    }
    /**
     * @desc 同步 
     * @return boolean
     * 一定时间内 只能解密一次 如果多次解密的话 会出现解密失败  解密频繁调用的报错
     */
    public function async_back()
    {
        if (empty($this->shopArr)) {
            return ['code' => 103, 'msg' => '参数错误,请联系管理员'];
        }
        // 获取增量订单
        set_time_limit(300);
        $time2  =  time();

        if (!empty($this->shopArr['mess_secret'])) {
            $time2 = ($this->shopArr['mess_secret'] + $this->interval_time);
        }
        // $time3  =   strtotime("-12 hour");
        // 间隔时间
        $time1  =   $time2 - $this->interval_time;

        /* $res    =   DB::table('flow_order_async')->where([['async_time', '>', $time1]])->first();
        if($res){ return true; }*/

        // $url    =   $this->config['url_api'];
        $url = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['end_updated_at']     =   $time2;
        $data['is_lucky_flag']      =   '0';
        $data['order_status']       =   '1';
        $data['page']               =   '1';
        $data['refund_status']      =   '1';
        $data['trade_type']      =   '0';
        $data['start_updated_at']   =   $time1;
        $data['timestamp']          =   $time2;
        $data['type']               =   'pdd.order.number.list.increment.get';
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);

        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        $this->root->info('传递的参数:' . json_encode($data) . '同步订单库---' . $content);
        $json       =   json_decode($content, true);
        DB::table('pdd_conf')->where('id', $this->pdd_id)->update(['mess_secret' => $time2]);
        // 获取到订单
        $this->root->info('同步订单库---' . $content);
        if (!$json || !isset($json['order_sn_increment_get_response']) || !isset($json['order_sn_increment_get_response']['total_count']) || $json['order_sn_increment_get_response']['total_count'] < 1) {
            $this->root->info('同步订单库失败---' . $content);
            return false;
        }
        $add    =   [];
        $model = new MainOrderRepostition();
        $allList = [];
        foreach ($json['order_sn_increment_get_response']['order_sn_list'] as $n) {
            if (!isset($n['order_sn'])) {
                $this->root->info('缺少订单信息---');
                continue;
            }
            if (DB::table('pdd_catch')->where('dist_orderno', $n['order_sn'])->first()) {
                continue;
            }
            $orderInfo  =   $this->getOrderInfo($n['order_sn']);
            if (!$orderInfo) {
                $this->root->info('获取用户身份信息失败---' . $n['order_sn']);
                continue;
            }
            $n['id_card_name'] = $orderInfo['id_card_name'];
            $n['id_card_num'] = $orderInfo['id_card_num'];
            $userInfo   =   $this->getUserInfo($n);
            // $userInfos = $this->getAllUserInfo($n, $allList);
            // $userInfo = $userInfos['data'];
            // $allList = $userInfos['allList'];
            $staus = '0';
            if (!$userInfo) {
                $staus = '60';
                $stausDesc = '失败单';
                $this->root->info('解密失败---' . $n['order_sn']);
                // continue;
            }

            $time   =   date('Y-m-d H:i:s');
            // if (!DB::table('pdd_catch')->where('dist_orderno', $n['order_sn'])->first()) {
            //     if(){

            //     }

            // }
            $insertOrder = [
                'dist_orderno' => $n['order_sn'],
                'dist_id' => $this->shopArr['id'],
                'dist_name' => $this->shopArr['remark'],
                'goods_code' => $n['item_list'][0]['outer_id'],
                'name' => !empty($userInfo['true_name']) ? $userInfo['true_name'] : '',
                'identity' => !empty($userInfo['idcard']) ? $userInfo['idcard'] : '',
                'contact' => !empty($userInfo['phone']) ? $userInfo['phone'] : '',
                'province' => $n['province'],
                'city' =>  $n['city'],
                'area' => $n['town'],
                'address' => !empty($userInfo['addr']) ? $userInfo['addr'] : '',
                'create_at' => date('Y-m-d H:i:s'),
                'created_at' => $n['created_time'],
                'shop_id' => $this->shopArr['shop_id']
            ];
            // 插入订单表中

            if (empty($staus) || $staus != 60) {
                $insertOrder['source'] = 11;
                $res = $model->orderSubmit($insertOrder);
                $this->log->info('抖音参数:' . json_encode($insertOrder) . '返回参数:' . json_encode($res));
            }
            $insertOrder['status'] = !empty($staus) ? $staus : '0';
            $insertOrder['status_desc'] = !empty($stausDesc) ? $stausDesc : '提交中';
            if (!empty($res['code']) && $res['code'] == 200) {
                $insertOrder['status'] = 1;
                $insertOrder['status_desc'] = '提交上游';
                $insertOrder['three_msg'] = !empty($res['msg']) ? $res['msg'] : '提交异常';
            }
            if (empty($res['code']) || $res['code'] != 200) {
                $insertOrder['status'] = 50;
                $insertOrder['status_desc'] = '提交失败';
                $insertOrder['three_msg'] = !empty($res['msg']) ? $res['msg'] : '提交异常';
            }
            if (!empty($insertOrder['source'])) {
                unset($insertOrder['source']);
            }
            $add[] = $insertOrder;
        }
        if ($add) DB::table('pdd_catch')->insert($add);

        return 1;
    }
    public function getOrderInfo($order_sn)
    {
        // return [
        //     'id_card_name' => mt_rand(00000, 999999),
        //     'id_card_num' => mt_rand(000000000, 999999999),
        // ];
        // $url    =   $this->config['url_api'];
        $url   = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   time();
        $data['type']               =   'pdd.order.virtual.information.get';
        $data['order_sn']           =   $order_sn;
        $data['sign']               =   $this->getSign($data);
        //dd($data);
        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);

        $content    =   get_url_content($url, $data, $header);
        $this->root->info('获取用户信息参数:' . ($data) . '返回结果:' . $content);
        $json       =   json_decode($content, true);

        if (!$json || !isset($json['order_virtual_information_response'])) {
            $this->root->info('订单获取失败---' . $content);
            return false;
        }

        return [
            'id_card_name' => $json['order_virtual_information_response']['id_card_name'],
            'id_card_num' => $json['order_virtual_information_response']['id_card_num'],
        ];
    }
    function getSign($params)
    {
        $arrSign    =   [];
        ksort($params);
        $rule       =  [];
        foreach ($params as $k => $v) {
            if (in_array($k, $rule) || $v == '') {
                continue;
            }

            if (is_array($v)) {
                $v = json_encode($v);
            }

            $arrSign[] = trim($k) . trim($v);
        }
        $strSign    =   implode('', $arrSign);
        $sign       =   strtoupper(md5($this->client_secret . $strSign . $this->client_secret));
        return $sign;
    }

    public function getAes($params)
    {
        $text   =   http_build_query($params, ' ', '&');
        $sign   =   openssl_encrypt($text, 'AES-256-ECB', $this->miss_secret);
        return $sign;
    }
    // 匹配解密后的数据
    public function getUserInfoDesc($n, $allList)
    {
        if (empty($n)  || empty($allList)) {
            return [];
        }
        $data = [];
        // 'true_name' => '',
        // 'idcard' => '',
        // 'phone' => '',
        // 'addr' => '',
        // 'name' => ''
        // ];
        $i = 0;
        foreach ($allList as $key => $v) {
            if ($n['true_name'] == $v['encrypted_data']) {
                $data['true_name'] = $v['decrypted_data'];
                $i++;
                continue;
            }
            if ($n['idcard'] == $v['encrypted_data']) {
                $data['idcard'] = $v['decrypted_data'];
                $i++;
                continue;
            }
            if ($n['phone'] == $v['encrypted_data']) {
                $data['phone'] = $v['decrypted_data'];
                $i++;
                continue;
            }
            if ($n['addr'] == $v['encrypted_data']) {
                $data['addr'] = $v['decrypted_data'];
                $i++;
                continue;
            }
            if ($n['name'] == $v['encrypted_data']) {
                $data['name'] = $v['decrypted_data'];
                $i++;
                continue;
            }
            if ($i >= 5) {
                break;
            }
        }
        return $data;
    }
    // 解密批量处理
    public function getAllUserInfo($list, $allList)
    {
        $data = [
            'true_name' => $list['id_card_name'],
            'idcard' => $list['id_card_num'],
            'phone' => $list['receiver_phone'],
            'addr' => $list['address'],
            'name' => $list['receiver_name']
        ];
        $res[$list['order_sn']] = $data;
        $allList = array_merge($allList, $res);
        return [
            'data' => $data,
            'allList' => $allList
        ];
    }
    // 解密
    public function getUserInfo($list)
    {
        // $c      =   json_encode([
        //     ['data_tag' => $list['order_sn'], 'encrypted_data' => $list['id_card_name']],
        //     ['data_tag' => $list['order_sn'], 'encrypted_data' => $list['id_card_num']],
        //     ['data_tag' => $list['order_sn'], 'encrypted_data' => $list['receiver_phone']],
        //     ['data_tag' => $list['order_sn'], 'encrypted_data' => $list['address']],
        //     ['data_tag' => $list['order_sn'], 'encrypted_data' => $list['receiver_name']],
        // ]);
        $resC = [];
        if (!empty($list)) {
            foreach ($list as $kye => $v) {
                $resC[] = ['data_tag' => $kye, 'encrypted_data' => $v['true_name']];
                $resC[] = ['data_tag' => $kye, 'encrypted_data' => $v['idcard']];
                $resC[] = ['data_tag' => $kye, 'encrypted_data' => $v['phone']];
                $resC[] = ['data_tag' => $kye, 'encrypted_data' => $v['addr']];
                $resC[] = ['data_tag' => $kye, 'encrypted_data' => $v['name']];;
            }
        }
        // var_dump($resC, 5555555555);
        if(empty($resC)){
            $this->log->info('解密数据为空d参数:' . json_encode($list) . '返回结果:' . json_encode($resC));
            return  false;
        }
        $c = json_encode($resC);
        if(empty($c)){
            $this->log->info('解密数据为空a参数:' . json_encode($list) . '返回结果:' . ($c));
            return false;
        }

        // $url    =   $this->config['url_api'];
        $url    = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   time();
        $data['type']               =   'pdd.open.decrypt.batch';
        $data['data_list']          =   $c;
        $data['sign']               =   $this->getSign($data);
        //dd($data);
        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);

        $content    =   get_url_content($url, $data, $header);

        $this->log->info('解密数据:' . $data . '返回结果:' . ($content));
        $json       =   json_decode($content, true);

        if (!$json || !isset($json['open_decrypt_batch_response']) || !isset($json['open_decrypt_batch_response']['data_decrypt_list'][4])) {
            $this->root->info('订单解密失败---' . $content);
            return false;
        }
        // 返回的解密的数组
        return !empty($json['open_decrypt_batch_response']['data_decrypt_list']) ? $json['open_decrypt_batch_response']['data_decrypt_list'] : [];
        return [
            'true_name' => !empty($json['open_decrypt_batch_response']['data_decrypt_list'][0]['decrypted_data']) ? $json['open_decrypt_batch_response']['data_decrypt_list'][0]['decrypted_data'] : '',
            'idcard' => !empty($json['open_decrypt_batch_response']['data_decrypt_list'][1]['decrypted_data']) ? $json['open_decrypt_batch_response']['data_decrypt_list'][1]['decrypted_data'] : '',
            'phone' => !empty($json['open_decrypt_batch_response']['data_decrypt_list'][2]['decrypted_data']) ? $json['open_decrypt_batch_response']['data_decrypt_list'][2]['decrypted_data'] : '',
            'addr' => !empty($json['open_decrypt_batch_response']['data_decrypt_list'][3]['decrypted_data']) ? $json['open_decrypt_batch_response']['data_decrypt_list'][3]['decrypted_data'] : '',
            'name' => !empty($json['open_decrypt_batch_response']['data_decrypt_list'][4]['decrypted_data']) ? $json['open_decrypt_batch_response']['data_decrypt_list'][4]['decrypted_data'] : '',
        ];
    }

    public function getOrderDetail($order_sn)
    {

        $url    =   $this->config['url_api'];
        $url   = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   time();
        $data['type']               =   'pdd.order.virtual.information.get';
        $data['order_sn']           =   $order_sn;
        $data['sign']               =   $this->getSign($data);
        //dd($data);
        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);

        $content    =   get_url_content($url, $data, $header);
        $json       =   json_decode($content, true);

        if (!$json || !isset($json['order_virtual_information_response'])) {
            $this->root->info('订单获取失败---' . $content);
            return false;
        }

        return [
            'id_card_name' => $json['order_virtual_information_response']['id_card_name'],
            'id_card_num' => $json['order_virtual_information_response']['id_card_num'],
        ];
    }

    function getAcToken()
    {
        $time   =   time();
        // $token  =   DB::table('order_access_token')->where('id', 1)->first();
        if ($this->access_token) {
            return $this->access_token;
        }
        // var_dump($this->shopArr['code'], 111111111111);
        // $code   =   DB::table('order_code')->where('id', 1)->first();
        $data['type']      =   'pdd.pop.auth.token.create';
        $data['client_id'] =   $this->client_id;
        $data['timestamp'] =   $time;
        $data['code']      =   $this->shopArr['code'];
        $data['data_type'] =   'JSON';
        $data['sign']      =   $this->getSign($data);
        $url               =   $this->url_api . '?' . http_build_query($data, ' ', '&');
        $content           =   get_url_content($url, []);
        $this->root->info('参数:' . json_encode($data) . '返回结果:' . $content);
        $json              =   json_decode($content, true);

        if (!$json || !isset($json['pop_auth_token_create_response']) || !isset($json['pop_auth_token_create_response']['access_token'])) {
            $this->root->info('获取token失败---' . $content);
            return ['code' => 106, 'msg' => '获取access_token失败'];
        }
        // $time2 = 60 * 60 * 24 * 364 + $time;
        DB::table('pdd_conf')->where(['id' => $this->pdd_id])->update(['params' => $json['pop_auth_token_create_response']['access_token']]);
        // DB::table('order_access_token')->where('id', 1)->update(['access_token' => $json['pop_auth_token_create_response']['access_token'], 'token_time' => $time2]);

        return $json['pop_auth_token_create_response']['access_token'];
    }

    function getCode()
    {
        $url = $this->url_code . '?' . 'response_type=code&client_id=' . $this->client_id . '&redirect_uri=' . $this->return_url . '&state=79';
        // var_dump($url);
        $content    =   get_url_content($url, []);
        $this->log->info($url);
        return $content;
    }

    /**
     * 所有订单
     * 间隔时间 * 5 
     * 这就是意味着 获取五次接口后
     *
     * @return void
     */
    public function orderList()
    {
        set_time_limit(300);

        $time2  =   time();
        $time1  =   $time2 - 60 * 60 * 24;

        // $url    =   $this->config['url_api'];
        $url = $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['end_confirm_at']     =   $time2;
        $data['order_status']       =   '1';
        $data['page']               =   '1';
        $data['trade_type']         =   '0';
        $data['refund_status']      =   '5';
        $data['start_confirm_at']   =   $time1;
        $data['timestamp']          =   $time2;
        $data['type']               =   'pdd.order.list.get';
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);

        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        $json       =   json_decode($content, true);
        dd($json);
    }
    // 发货
    public function subOrder($order)
    {
        // 449 联通自由物流
        $code = '120';
        if (!empty($order['express_company']) && $order['express_company'] == 'EMS') {
            $code = '118';
        } else if (!empty($order['express_company']) && strstr($order['express_company'], '顺丰')) {
            $code = '44';
        } elseif (!empty($order['express_company']) && strstr($order['express_company'], 'EMS')) {
            $code = '118';
        } elseif (!empty($order['express_company']) && strstr($order['express_company'], '邮政')) {
            $code = '118';
        }
        $time   =   time();
        $url    =  $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   $time;
        $data['type']               =   'pdd.logistics.online.send';
        $data['order_sn']           =   $order['dist_orderno'];
        $data['logistics_id']       =   $code;
        $data['tracking_number']    =   $order['express_number'];
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);

        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        $this->log->info('拼多多发货参数:' . json_encode($data) . '返回结果' . $content);
        $json       =   json_decode($content, true);
        return $json;
    }
    // 失败处理
    public function failOrder($order)
    {
        $time   =   time();
        $url    =  $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   $time;
        $data['type']               =   'pdd.order.note.update';
        $data['order_sn']           =   $order['dist_orderno'];
        $data['note']       =   $order['three_msg'];
        $data['tag']    =   '1';
        $data['tag_name']     = '红色';
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);
        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        $json       =   json_decode($content, true);
        $this->root->info('失败传递参数:' . $data . '返回参数:' . $content);
        return $json;
    }

    public function getCompany($order)
    {
        $time   =   time();
        $url    =  $this->url_api;
        $data['access_token']       =   $this->access_token;
        $data['client_id']          =   $this->client_id;
        $data['data_type']          =   'JSON';
        $data['timestamp']          =   $time;
        $data['type']               =   'pdd.logistics.companies.get';
        $data['version']            =   $this->version;
        $data['sign']               =   $this->getSign($data);

        $header =   ["Content-Type: application/json", "Accept: application/json"];

        $data       =   json_encode($data);
        $content    =   get_url_content($url, $data, $header);
        $json       =   json_decode($content, true);
        // dd($json);
        return $json;
        var_dump($json);
    }
}
